package com.rock.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.lang3.time.DateFormatUtils;

public class DateUtil extends org.apache.commons.lang3.time.DateUtils {
	private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
			"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss",
			"yyyy.MM.dd HH:mm", "yyyy.MM" };
	private static SimpleDateFormat WEEK_SIMPLE_FORMAT = new SimpleDateFormat("u");

	public static String getDate() {
		return getDate("yyyy-MM-dd");
	}

	public static String getDate(String pattern) {
		return DateFormatUtils.format(new Date(), pattern);
	}

	public static String formatDate(Date date, Object... pattern) {
		if (date == null) {
			return "";
		}
		String formatDate = null;
		if ((pattern != null) && (pattern.length > 0)) {
			formatDate = DateFormatUtils.format(date, pattern[0].toString());
		} else {
			formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
		}
		return formatDate;
	}

	public static String formatDateTime(Date date) {
		return formatDate(date, new Object[] { "yyyy-MM-dd HH:mm:ss" });
	}

	public static String getTime() {
		return formatDate(new Date(), new Object[] { "HH:mm:ss" });
	}

	public static String getDateTime() {
		return formatDate(new Date(), new Object[] { "yyyy-MM-dd HH:mm:ss" });
	}

	public static String getYear() {
		return formatDate(new Date(), new Object[] { "yyyy" });
	}

	public static String getMonth() {
		return formatDate(new Date(), new Object[] { "MM" });
	}

	public static String getDay() {
		return formatDate(new Date(), new Object[] { "dd" });
	}

	public static String getWeek() {
		return formatDate(new Date(), new Object[] { "E" });
	}

	public static String getWeekNumber() {
		return WEEK_SIMPLE_FORMAT.format(new Date());
	}

	public static Date parseDate(Object str) {
		if (str == null) {
			return null;
		}
		try {
			return parseDate(str.toString(), parsePatterns);
		} catch (ParseException e) {
		}
		return null;
	}

	public static long pastDays(Date date) {
		long t = new Date().getTime() - date.getTime();
		return t / 86400000L;
	}

	public static long pastHour(Date date) {
		long t = new Date().getTime() - date.getTime();
		return t / 3600000L;
	}

	public static long pastMinutes(Date date) {
		long t = new Date().getTime() - date.getTime();
		return t / 60000L;
	}

	public static String formatDateTime(long timeMillis) {
		long day = timeMillis / 86400000L;
		long hour = timeMillis / 3600000L - day * 24L;
		long min = timeMillis / 60000L - day * 24L * 60L - hour * 60L;
		long s = timeMillis / 1000L - day * 24L * 60L * 60L - hour * 60L * 60L - min * 60L;
		long sss = timeMillis - day * 24L * 60L * 60L * 1000L - hour * 60L * 60L * 1000L - min * 60L * 1000L
				- s * 1000L;
		return (day > 0L ? day + "," : "") + hour + ":" + min + ":" + s + "." + sss;
	}

	public static String formatDateOts(Date date) {
		return formatDate(date, new Object[] { "yyyyMMddHHmmss000000" });
	}

	public static double getDistanceOfTwoDate(Date before, Date after) {
		long beforeTime = before.getTime();
		long afterTime = after.getTime();
		return (afterTime - beforeTime) / 86400000L;
	}

	public static String addStringDays(String date, int count) {
		return formatDate(addDays(parseDate(date), count), new Object[0]);
	}

	public static String getToday() {
		return formatDate(new Date(), new Object[] { "yyMMdd" });
	}

	public static void main(String[] args) {
		Date date=new Date();
		System.out.println("日期格式化为：yyyy-MM-dd:"+DateUtil.formatDate(date, "yyyy-MM-dd"));
		System.out.println(DateUtil.formatDate(date));
		System.out.println("格式化日期时间，24小时制："+DateUtil.formatDate(date, "yyyy-MM-dd HH:mm:ss"));
		System.out.println("格式化日期时间，12小时制："+DateUtil.formatDate(date, "yyyy-MM-dd hh:mm:ss"));
		System.out.println("获取当前日期，格式化为yyyy-MM-dd："+DateUtil.getDate());
		System.out.println("获取当天是周几，如星期四："+DateUtil.getWeek());
		System.out.println("获取当天是周几，如4，阿拉伯数字："+DateUtil.getWeekNumber());

	}
}
